// --------------------------------------------------------------------------------------------------------------------
// <copyright file="PhoneApplicationPageExtensions.cs" company="XamlNinja">
//   2011 Richard Griffin and Ollie Riches
// </copyright>
// <summary>
//   PhoneApplicationPageExtensions defines the methods that you can call from the Phone App Page.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace WP7Contrib.View.Controls.Tombstoner
{
    using System;
    using System.Linq;
    using System.Windows.Controls;

    using Microsoft.Phone.Controls;

    using WP7Contrib.View.Controls.Extensions;

    public static class PhoneApplicationPageExtensions
    {
        public static void PersistState(this PhoneApplicationPage page)
        {
            try
            {
                foreach (var tombstoner in TombstonerRepository.GetAll())
                {
                    var localTombstoner = tombstoner;
                    foreach (var control in page.DescendantsAndSelf().Where(d => d.GetType() == localTombstoner.ControlType).Cast<Control>())
                    {
                        tombstoner.Persist(control, page);
                    }
                }
            }
            catch (Exception)
            {
            }
        }

        public static void PersistState(this PhoneApplicationPage page, params Control[] controls)
        {
            try
            {
                if (controls == null)
                {
                    return;
                }

                foreach (var control in controls)
                {
                    var tombstoner = TombstonerRepository.Get(control.GetType());
                    if (tombstoner != null)
                    {
                        tombstoner.Persist(control, page);
                    }
                }
            }
            catch (Exception)
            {
            }
        }

        public static void RestoreState(this PhoneApplicationPage page)
        {
            try
            {
                foreach (var tombstoner in TombstonerRepository.GetAll())
                {
                    var localTombstoner = tombstoner;
                    foreach (var control in page.DescendantsAndSelf().Where(d => d.GetType() == localTombstoner.ControlType).Cast<Control>())
                    {
                        tombstoner.Restore(control, page);
                    }
                }
            }
            catch (Exception)
            {
            }
        }

        public static void RestoreState(this PhoneApplicationPage page, params Control[] controls)
        {
            try
            {
                if (controls == null)
                {
                    return;
                }

                foreach (var control in controls)
                {
                    var tombstoner = TombstonerRepository.Get(control.GetType());
                    if (tombstoner != null)
                    {
                        tombstoner.Restore(control, page);
                    }
                }
            }
            catch (Exception)
            {
            }
        }
    }
}